package fy.mdm.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.soa.eis.adapter.framework.message.IMsgObject;
import com.soa.eis.adapter.framework.message.impl.GroupRecord;
import com.soa.eis.adapter.framework.message.impl.MsgObject;
import com.soa.eis.adapter.framework.requester.IServiceRequester;
import com.soa.eis.adapter.framework.requester.impl.BaseServiceRequester;

import fy.config.FyConfig;
import fy.mdm.bean.NewCustomerCreditBean;
import fy.util.DBUtil;
public class NewCustomerCreditDao {
	private static Log log = LogFactory.getLog(NewCustomerCreditDao.class);
	private NewCustomerCreditBean NewCustomerCreditBean;
	//写入MDM数据库
	public boolean getStateReturn() {
		try {
			Connection conn = null;
			PreparedStatement pst = null;
			ResultSet rs = null;
			IServiceRequester Requester = BaseServiceRequester.getInstance();
			List<GroupRecord> grs1 = new ArrayList<GroupRecord>();//多层用
			List<GroupRecord> grs2 = new ArrayList<GroupRecord>();
			// 定义请求报文对象
			IMsgObject reqMo = new MsgObject(IMsgObject.MOType.initSR);
			String SourceSysID = "06004"; // 请求方(发布方)
			String ServiceID = "06001000000007";// 响应方(发布方)
			// 获取系统时间
			Date date = new Date();
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
			String format = simpleDateFormat.format(date);
			// 对请求报文对象进行设置
			reqMo.setSourceSysID(SourceSysID); // 设置报文 请求方系统号
			reqMo.setSerialNO(format.substring(0, 8) + SourceSysID
					+ String.format("%06d", ++MaterialRequestNoTimerTask.requestNo));// 设置报文流水号,第三段表示今天发送的第几的一次报文
			reqMo.setServiceID(ServiceID); // 设置报文 提供方服务号
			reqMo.setServiceDateTime(format); // 设置报文 日期时间
			reqMo.setReqValue("BOID","SOA_MDM_CUSTOMER_F" );
			reqMo.setReqValue("ControlType", "5");
			GroupRecord gr1 = new GroupRecord();
			conn = DBUtil.connect(FyConfig.getOA_DB_CONFIG());
			conn.setAutoCommit(false);
			log.info("NewCustomerCreditBean.getSummaryId()= "+NewCustomerCreditBean.getSummaryId());
			 pst = conn.prepareStatement("select L.CODE,H.CREATE_DATE from formmain_25584 T LEFT JOIN ORG_MEMBER L on T.MODIFY_MEMBER_ID = L.ID LEFT JOIN COL_SUMMARY H ON T.ID = H.FORM_RECORDID  where H.ID = ?");
			 //pst = conn.prepareStatement("select L.CODE,H.CREATE_DATE from formmain_21108 T LEFT JOIN ORG_MEMBER L on T.MODIFY_MEMBER_ID = L.ID LEFT JOIN COL_SUMMARY H ON T.ID = H.FORM_RECORDID  where H.ID = ?");//测试
			pst.setLong(1,NewCustomerCreditBean.getSummaryId());
			rs = pst.executeQuery();
			while (rs.next()) {
				gr1.setFieldValue("APPROVER",rs.getString(1));
				//gr1.setFieldValue("CREATE_TIME",rs.getString(2));
				log.info("APPROVER = "+rs.getString(1));
			}
			pst = conn.prepareStatement("select  l.CODE from ORG_MEMBER l where l.ID = ?");
			pst.setLong(1,NewCustomerCreditBean.getApplier());
			rs = pst.executeQuery();
			log.info("getApplier ----->>"+NewCustomerCreditBean.getApplier());
			while (rs.next()) {
				gr1.setFieldValue("CREATOR",rs.getString(1));
				log.info("CREATOR = "+rs.getString(1));
			}
			gr1.setName("ROW");
			gr1.setFieldValue("MDM_CODE",NewCustomerCreditBean.getCUSTOME_CODE());
			gr1.setFieldValue("CUSTOME_NAME",NewCustomerCreditBean.getCustomerName());
			gr1.setFieldValue("COUNTRY",getEnumValue("客户国别",NewCustomerCreditBean.getCustomerNationality()));//-6451029771514458423
			gr1.setFieldValue("CUSTOMER_ATTRIBUTE",getEnumValue("客户属性",NewCustomerCreditBean.getCustomerAttribute()));//-5249437150005932910
			gr1.setFieldValue("CUSTOMER_TYPE",getEnumValue("客户系别",NewCustomerCreditBean.getAccountType()));//-7165701771049090154
			gr1.setFieldValue("BIG_TYPE",getEnumValue("客户类型1",NewCustomerCreditBean.getCustomerType()));//5221610432387173710
			gr1.setFieldValue("DAYS",getEnumValue("信用天数",NewCustomerCreditBean.getCreditDays()));//6499889681206496350
			gr1.setFieldValue("SETTLEMENT",getEnumValue("新客户授信-结算",NewCustomerCreditBean.getSettlementMethod()));//-505727333891551998
			gr1.setFieldValue("TAX_TYPE",getEnumValue("是否",NewCustomerCreditBean.getTaxRebates()));//-1320082243332587112
			gr1.setFieldValue("REMARK","OA");
			gr1.setFieldValue("SMALL_TYPE","配套");
			gr1.setFieldValue("STATUS","有效");
			gr1.setFieldValue("TRANSACTION_TYPE","2");

			gr1.setFieldValue("SALES_AREAS","");
			gr1.setFieldValue("PARENT_CUSTOMER","");
			gr1.setFieldValue("COMMISION_RATE","");
			gr1.setFieldValue("TS_DEPT_RATE","");
			gr1.setFieldValue("ACCEPT_BILL_DAYS","");
			gr1.setFieldValue("CUSTOMER_ASSIGNMENT","");
			gr1.setFieldValue("CUSTOMER_FUNCTION","");
			gr1.setFieldValue("TYPE","");
			gr1.setFieldValue("AREA","");
			gr1.setFieldValue("F_A","");
			gr1.setFieldValue("F_B","");
			gr1.setFieldValue("F_C","");
			gr1.setFieldValue("F_D","");
			gr1.setFieldValue("F_E","");
			grs1.add(gr1);
			GroupRecord gr2 = new GroupRecord();
			gr2.setName("DATAS");
			gr2.setSubGroups("value", grs1);
			grs2.add(gr2);
			reqMo.setReqGroupRecord(grs2);
			log.info("reqMo:"+reqMo);
			IMsgObject respMo = null;
			respMo = Requester.execute(reqMo);//发布报文
			log.info("respMo:"+respMo);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
		return true;
	}
	
	//查询下拉值
	public String getUnitCode(String code, String temp) {
		Connection conn = null;
		PreparedStatement pst = null;
		ResultSet rs = null;

		try {
			conn = DBUtil.connect(FyConfig.getOA_DB_CONFIG());
			pst = conn.prepareStatement("SELECT c.SHOWVALUE FROM  CTP_ENUM_ITEM  c WHERE c.ENUMVALUE=? AND c.REF_ENUMID=?");
			pst.setString(1, code);
			pst.setString(2, temp);
			rs = pst.executeQuery();
			if (rs.next()) {
				code = rs.getString(1);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(rs, pst);
		}
		return code;
	}
	public String getEnumValue(String enumName,String value)throws Exception{
		Connection conn = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		conn = DBUtil.connect(FyConfig.getOA_DB_CONFIG());
		conn.setAutoCommit(false);
		pst = conn.prepareStatement("select l.SHOWVALUE from CTP_ENUM t left join CTP_ENUM_ITEM l on t.ID = l.REF_ENUMID WHERE l.ENUMVALUE = ? AND t.ENUMNAME = ? AND  t.IFUSE = 'Y'");
		pst.setString(1,value);
		pst.setString(2,enumName);
		rs = pst.executeQuery();
		while (rs.next()) {
			log.info("getEnumValue = "+rs.getString(1));
			return rs.getString(1);
		}
		DBUtil.close(rs);
		return "";
	}
	public NewCustomerCreditBean getNewCustomerCreditBean() {
		return NewCustomerCreditBean;
	}

	public void setNewCustomerCreditBean(NewCustomerCreditBean newCustomerCreditBean) {
		NewCustomerCreditBean = newCustomerCreditBean;
	}
	
}
